package com.corgi.quartz.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * @title: QuartzAdaptableJobFactory
 * @description:
 * @author: dengmiao
 * @create: 2019-11-11 15:08
 **/
@Component("quartzAdaptableJobFactory")
public class QuartzAdaptableJobFactory extends AdaptableJobFactory {

    /**
     * AutowireCapableBeanFactory可以将一个对象添加到spring ioc容器中 并完成该对象的注入
     */
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    /**
     * 该方法需要将实例化的任务对象手动添加到spring ioc容器中
     * @param bundle
     * @return
     * @throws Exception
     */
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object instance = super.createJobInstance(bundle);
        // 将任务对象添加到spring ioc容器中 并完成注入
        this.autowireCapableBeanFactory.autowireBean(instance);
        return instance;
    }
}
